<template>
  <div class="app-container">
    <el-form :model="refreshAdmin" ref="refreshAdminIdForm" v-show="showSearch" :inline="true">
      <el-form-item label="上级编号" prop="parentId">
        <el-input
          v-model="refreshAdmin.parentId"
          placeholder="请输入上级编号"
          clearable
          size="small"
          style="width: 240px"
          v-on:input="resetShopIdQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="danger" icon="el-icon-caret-right" size="mini" @click="handleRefreshAdmin">上级同步</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetAdminIdQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-form :model="refreshShop" ref="refreshShopIdForm" v-show="showSearch" :inline="true">
      <el-form-item label="机构编号" prop="storeId">
        <el-input
          v-model="refreshShop.storeId"
          placeholder="请输入机构编号"
          clearable
          size="small"
          style="width: 240px"
          v-on:input="resetAdminIdQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="danger" icon="el-icon-caret-right" size="mini" @click="handleRefreshShopId">机构同步</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetShopIdQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="上级编号" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入上级编号"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构编号" prop="storeId">
        <el-input
          v-model="queryParams.storeId"
          placeholder="请输机构店编号"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构名称" prop="channelName">
        <el-input
          v-model="queryParams.channelName"
          placeholder="请输入机构名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="机构状态"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery">
          <el-option v-for="item in option" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-caret-right"
          size="mini"
          @click="handleRefresh"
        >批量同步
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
        >批量删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-caret-right"
          size="mini"
          :disabled="multiple"
          @click="handleRefreshAll"
        >同步全机构
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="partnerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="上级编号" prop="parentId" align="center"/>
      <el-table-column label="机构编号" prop="storeId" align="center"/>
      <el-table-column label="机构名称" prop="channelName" align="center"/>
      <el-table-column label="联系人电话" prop="phone" align="center"/>
      <el-table-column label="机构创建时间" prop="createTime" align="center"/>
      <el-table-column label="机构状态" align="center">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" plain v-if="scope.row.storeId == null">未初始</el-button>
          <el-button type="primary" size="mini" plain v-if="scope.row.storeId != null">已初始</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-caret-right"
            @click="handleRefresh(scope.row)"
          >同步
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>
  </div>
</template>

<script>
import {getPartner, refresh, refreshCustom, deletePartner} from "@/api/system/partner.js";

export default {
  name: "circleView",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非多个禁用
      multiple: false,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表单数据
      partnerList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: undefined,
        storeId: undefined,
        channelName: undefined,
        phone: undefined,
        status: 0,
      },
      option: [
        {
          value: 0,
          label: '全部状态'
        },
        {
          value: 1,
          label: '已初始'
        },
        {
          value: 2,
          label: '未初始'
        }
      ],
      //同步
      refreshAdmin: {
        parentId: undefined,
      },
      refreshShop: {
        storeId: undefined,
      },
    };
  },
  created() {
    this.getList();
    this.loading = false;
    this.queryParams.status = 1
  },
  methods: {
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      getPartner(this.queryParams).then(
        response => {
          this.partnerList = response.result;
          this.total = response.totalCount;
          this.loading = false;
        }
      ).catch(error => {
        console.log(error)
      });
      ;
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    resetAdminIdQuery() {
      this.resetForm("refreshAdminIdForm");
    },
    resetShopIdQuery() {
      this.resetForm("refreshShopIdForm");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = selection.length
    },
    /** 同步 */
    handleRefresh(row) {
      debugger
      let idList = row.id != null ? [row.id] : this.ids;
      this.$confirm('是否确认同步编号为"' + idList + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return refresh(idList);
      }).then(() => {
        this.getList();
        this.msgSuccess("同步成功");
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      let idList = row.id != null ? [row.id] : this.ids;
      this.$confirm('是否确认删除编号为"' + idList + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return deletePartner(idList);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    handleRefreshAdmin() {
      const data = this.refreshAdmin;
      if (data.parentId == null) {
        return
      }
      this.$confirm('是否确认同步机构编号为"' + data.parentId + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return refreshCustom(data);
      }).then(() => {
        this.getList();
        this.msgSuccess("同步成功");
      })
    },

    handleRefreshShopId() {
      const data = this.refreshShop;
      if (data.storeId == null) {
        return
      }
      this.$confirm('是否确认同步店铺编号为"' + data.storeId + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return refreshCustom(data);
      }).then(() => {
        this.getList();
        this.msgSuccess("同步成功");
      })
    },

    handleRefreshAll() {
      const data = {'special': true}
      this.$confirm('是否确认同步全部机构', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return refreshCustom(data);
      })
    },
  }
}
;
</script>
